<?php

namespace app\admin\model;

use think\Model;

/**
 * Order
 */
class Order extends Model {
    // 表名
    protected $name = 'order';


    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;

    protected $append = ['shipment_status_text', 'shipment_status_update_time_text', 'option_text'];

    /**
     * 获取订单选项
     */
    public function getOptionTextAttr($value, $data){
        return match ($data['option']){
            0 => '只要现货',
            3 => '接受预定3天',
            7 => '接受预定7天',
            14 => '接收预定14天'
        };
    }

    /**
     * 获取出货状态的更新时间
     */
    public function getShipmentStatusUpdateTimeTextAttr($value, $data){
        if(empty($data['shipment_status_update_time'])){
            return date('m-d H:i', $data['place_order_time']);
        }else{
            return date('m-d H:i', $data['shipment_status_update_time']);
        }
    }

    /**
     * 获取订单的出货状态
     */
    public function getShipmentStatusTextAttr($value, $data) {
        $status = [
            -1 => '取消订单',
            0 => '准备下单',
            1 => '已下单',
            2 => '现货',
            3 => '付款预定',
            4 => '口头预定',
            5 => '待取货',
            6 => '已提货',
            7 => '入库',
            8 => '韩国已发货',
            9 => '待定',
            10 => '退款',
            11 => '换货'
        ];
        return $status[$data['shipment_status']];
    }

    public function getSupplementImagesAttr($value): array {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setSupplementImagesAttr($value): string {
        return is_array($value) ? implode(',', $value) : $value;
    }

    /**
     * 关联user表
     */
    public function user() {
        return $this->hasOne('User', 'id', 'user_id');
    }
}